package com.example.test_1;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class demoRecycleView extends AppCompatActivity {

    private RecyclerView listRv;

    private String[] names = {"小猫","哈士奇","小黄鸭","老虎","大象"};
    private String[] introduces = {"猫，属于猫科动物，分家猫、野猫，是全世界家庭中较为广泛的宠物",
            "西伯利亚雪橇犬，常见别名哈士奇，昵称二哈",
            "鸭子的体型相对较小，小黄鸭小黄鸭小黄鸭",
            "老虎老欧胡楼上的卡德加哈技术大会按时打卡数据的黑暗时代就卡死的",
            "啊什么的哈师大设计的肯定会开啥会看到的撒赖扩大红军等哈说两句话大红色的"};
    private int[] icons = {R.drawable.lu1,R.drawable.lu2,R.drawable.hs3,R.drawable.shui,R.drawable.tiank};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_recycle_view);
        listRv = listRv.findViewById(R.id.recycleView);
        //recyclerView设置一个方向，默认给一个垂直方向
        listRv.setLayoutManager(new LinearLayoutManager(this));
        //然后设置一个Adapter
        listRv.setAdapter(new MyAdapter());
    }

    //创建Adapter  创建完ViewHolder给RecyclerView.Adapter一个泛型<MyAdapter.ViewHolder>
    //然后在实现几个方法
    class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
        //创建ViewHolder
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            //加载布局
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list2, null);
            return new ViewHolder(view);
        }
        //绑定ViewHolder 绑定数据
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

            holder.iconTv.setImageResource(icons[position]);
            holder.nameTv.setText(names[position]);
            holder.introduceTv.setText(introduces[position]);
        }
        //列表返回多少条数据
        @Override
        public int getItemCount() {
            return names.length;
        }
        //创建一个ViewHolder
        class ViewHolder extends RecyclerView.ViewHolder{
            ImageView iconTv;
            TextView nameTv,introduceTv;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                iconTv = itemView.findViewById(R.id.ivv);
                introduceTv = itemView.findViewById(R.id.introduce);
                nameTv = itemView.findViewById(R.id.name);
            }
        }
    }
}